package com.zdxk.msgPush.msg.subject;

import com.zdxk.msgPush.msg.observer.interfaces.IObserver;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

public class StockSubject extends AbstractSubject{

    private static volatile StockSubject instance;

    public static StockSubject getInstance(){
        if(instance == null){
            synchronized (StockSubject.class){
                if (instance == null){
                    instance = new StockSubject();
                }
            }
        }
        return instance;
    }

    private StockSubject(){

    }

    private static ConcurrentLinkedQueue<String> msgQueue = new ConcurrentLinkedQueue<>();

    private Map<String,Integer> tscodeTimesMap = new ConcurrentHashMap<>();

    public static void addMsg2Queue(String msg){
        msgQueue.add(msg);
    }

    @Override
    public boolean addObserver(IObserver observer) {
        getObserverMap().put(observer.getObserverId(),observer);
        return true;
    }

    @Override
    public IObserver removeObserver(String observerId) {
        return getObserverMap().remove(observerId);
    }

    @Override
    public List<String> notifyAllObserver(String msg) {
        List<String> notifyObserverIdList = new ArrayList<>();
        for(IObserver observer : getObserverMap().values()){
            if(observer.isMine(msg)){
                observer.doNotify(msg);
                notifyObserverIdList.add(observer.getObserverId());
            }

        }
        return notifyObserverIdList;
    }
}
